using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using InfraStructureLayer;
using BusinessLayer;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace WebControls
{
    public class ShadowFaxHttpModule : IHttpModule
    {

        #region IHttpModule Members

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication m_App = (HttpApplication)sender;
            HttpContext m_Context = m_App.Context;
            RewriteUrl(m_Context);

        }
        public void LoadParameters()
        {
            if (CurrentContext.Parameters == null)
            {
                busParameters insBusParameters = new busParameters();
                DataTable insDataTableParameters = insBusParameters.SelectParameters();
                Hashtable insHashtableParameters = new Hashtable();
                foreach (DataRow dr in insDataTableParameters.Rows)
                {
                    insHashtableParameters.Add(dr["ParameterName"].ToString(), dr["ParameterValue"].ToString());
                }
                CurrentContext.Parameters = insHashtableParameters;
            }
        }
        
        public void RewriteUrl(HttpContext context)
        {

            LoadParameters();
            string urlWithoutLanguage = "";
            string requestedPath=context.Request.Path;
            string rewritedUrl = "";
            if (requestedPath.ToLower().IndexOf("admin") == -1 && requestedPath.ToLower().IndexOf("utils") == -1)
            {
                if (requestedPath.ToLower().IndexOf("syndicationservice.asmx") > -1)
                {
                    context.RewritePath("/Rss/RssBlog.ashx", false);
                }

                if (requestedPath.StartsWith("/en/"))
                {
                    context.Items["LanguageId"] = 2;
                    urlWithoutLanguage = requestedPath.Remove(0, 3);
                    rewritedUrl = GenerateUrl(urlWithoutLanguage, context);
                    context.RewritePath(rewritedUrl, false);
                }
                else if (requestedPath.StartsWith("/tr/"))
                {
                    context.Items["LanguageId"] = 1;
                    urlWithoutLanguage = requestedPath.Remove(0, 3);
                    rewritedUrl = GenerateUrl(urlWithoutLanguage, context);
                    context.RewritePath(rewritedUrl, false);
                }
                else
                {
                    string s = requestedPath;
                    string address = s.Substring(s.LastIndexOf("/") + 1).ToLower().Replace(".aspx", "");
                    urlWithoutLanguage = address;
                    if (IsGuid(address) || address.StartsWith("category") || address.StartsWith("archive") || address.StartsWith("album") || address.StartsWith("photo"))
                    {
                        rewritedUrl = GenerateUrl(urlWithoutLanguage, context);
                        context.RewritePath(rewritedUrl, false);
                    }
                }
            }
        }
        
        private string GenerateUrl(string s,HttpContext context)
        {
            string address = s.Substring(s.LastIndexOf("/") + 1).ToLower().Replace(".aspx", "");
            if (address.StartsWith("category"))
            {
                string guid = address.Replace("category","");
                if (IsGuid(guid))
                {
                    context.Items["QueryStringCategory"] = guid;
                }
                if (s.ToLower().StartsWith("/articles/"))
                {
                    return "/Articles/default.aspx";
                }
                if (s.ToLower().StartsWith("/blog/"))
                {
                    return "/Blog/default.aspx";
                }
                if (s.ToLower().StartsWith("/news/"))
                {
                    return "/News/default.aspx";
                }
            }
            else if (address.StartsWith("archive"))
            {
                string guid = address.Replace("archive", "");
                if (IsGuid(guid))
                {
                    context.Items["QueryStringArchive"] = guid;
                }
                if (s.ToLower().StartsWith("/articles/"))
                {
                    return "/Articles/default.aspx";
                }
                if (s.ToLower().StartsWith("/blog/"))
                {
                    return "/Blog/default.aspx";
                }
                if (s.ToLower().StartsWith("/news/"))
                {
                    return "/News/default.aspx";
                }
            }
            else if (address.StartsWith("album"))
            {
                string guid = address.Replace("album", "");
                if (IsGuid(guid))
                {
                    context.Items["QueryStringAlbum"] = guid;
                }
                if (s.ToLower().StartsWith("/photos/"))
                {
                    return "/photos/default.aspx";
                }
            }
            else if (address.StartsWith("photo"))
            {
                string guid = address.Replace("photo", "");
                if (IsGuid(guid))
                {
                    context.Items["QueryStringPhoto"] = guid;
                }
                if (s.ToLower().StartsWith("/photos/"))
                {
                    return "/photos/default.aspx";
                }
            }
            else if (IsGuid(address))
            {
                string guid = address;
                context.Items["QueryStringId"] = guid;
                if (s.ToLower().StartsWith("/articles/"))
                {
                    return "/Articles/articleDetail.aspx";
                }
                if (s.ToLower().StartsWith("/blog/"))
                {
                    return "/Blog/entryDetail.aspx";
                }
                if (s.ToLower().StartsWith("/news/"))
                {
                    return "/News/NewsDetail.aspx";
                }
            }
            return s;
        }
        private bool IsGuid(string s)
        {
            Regex r = new Regex(@"^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$");
            return r.IsMatch(s);
        }
        #endregion
    }
}
